using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Model.Page;
using Keep.Service;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class MusicController : ControllerBase
{
    public class MusicRequest
    {
        public string? path { get; set; }
        public string? oldname { get; set; }
        public string? newname { get; set; }
        public string? name { get; set; }
        public string? selname { get; set; }
    }

    private readonly IFileService fileService;
    private readonly IMusicOfListService musicOfListService;

    public MusicController(IFileService fileService, IMusicOfListService musicOfListService)
    {
        this.fileService = fileService;
        this.musicOfListService = musicOfListService;
    }

    [HttpPost(Name = "DelMusic")]
    public async Task<ActionResult<ResultData>> DelMusic(MusicRequest musicRequest)
    {
        Log.Warning("IN =>   path=" + musicRequest.path + "  name=" + musicRequest.name);
        bool sys = await Task.Run(() => fileService.DelFile(musicRequest.path, Static.MusicPath, musicRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    //未启用
    [HttpPost(Name = "EditMusicDirOrName")]
    public async Task<ActionResult<ResultData>> EditMusicDirName(MusicRequest musicRequest)
    {
        Log.Warning("IN => path=" + musicRequest.path + "    oldname=" + musicRequest.oldname + "    newname=" + musicRequest.newname);
        bool sys = await Task.Run(() => fileService.EditFileDirOrName(musicRequest.path, Static.MusicPath, musicRequest.oldname, musicRequest.newname));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }

    [HttpPost(Name = "GetMusicPathInfo")]
    public async Task<ActionResult<ResultData>> GetMusicPathInfo()
    {
        Log.Warning("IN => ");
        PageResult<FileDiy> sys = await Task.Run(() => fileService.GetMusicPathInfo(1, 9999999));
        Log.Warning("OUT => " + sys.ToString() + "  数量:" + sys.Rows.Count);
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "GetNoUseMusic")]
    public async Task<ActionResult<ResultData>> GetNoUseMusic()
    {
        Log.Warning("IN => ");
        PageResult<FileDiy> sys = await Task.Run(() => fileService.GetMusicPathInfo(1, 9999999));
        Log.Warning("OUT => " + sys.ToString() + "  数量:" + sys.Rows.Count);
        if (sys.Rows != null && sys.Rows.Count > 0)
        {
            //排除掉已经被使用的音乐
            List<MusicOfList> list = musicOfListService.SelMusicOfList(new MusicOfList());
            if (list != null && list.Count > 0)
            {
                for (int i = sys.Rows.Count - 1; i >= 0; i--)
                {
                    for (int p = 0; p < list.Count; p++)
                    {
                        if (sys.Rows[i].name.Equals(list[p].name))
                        {
                            sys.Rows.RemoveAt(i);
                        }
                    }
                }
            }
        }
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "SelMusicByName")]
    public async Task<ActionResult<ResultData>> SelMusicByName(MusicRequest musicRequest)
    {
        Log.Warning("IN => " + musicRequest.selname);
        List<FileDiy> data = await Task.Run(() => fileService.SelFileByName(Static.MusicPath, musicRequest.selname));
        Log.Warning("OUT => " + data);
        return Ok(new ResultData(Static.Enable, data));
    }

    [HttpPost(Name = "UploadMusic")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadMusic(List<IFormFile> file, string? fileurl)
    {
        try
        {
            if (file.Count > 0)
            {
                if (String.IsNullOrEmpty(fileurl))
                {
                    fileurl = Request.Form["fileurl"];
                }
                Log.Warning("UploadMusic => " + fileurl);
                fileService.UploadFile(file, fileurl, Static.MusicPath);
                return Ok(new ResultData(Static.Enable, file[0].FileName));
            }
            else
            {
                return BadRequest("没有发现上传文件");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }
}
